package javachongkao.面向对象下.职工排序题;

import java.util.*;
//7-16 职工排序题
//1．    为某保险公司设计一个职工管理系统，其中职工类的属性有：职工编号，姓名，性别，团体险业绩，个体险业绩；方法有：
//每个属性对应的set，get方法；
//不带参数的构造方法；
//带参数的构造方法，完成对职工属性的初始化；
//该类实现接口Comparable，完成对职工总业绩的比较。
//
//2．    设计一个类，实现Comparator接口，完成对团体险业绩的比较；
//
//3．    在Main类中，创建一个职工的线性表，分别完成对职工线性表按照总业绩升序排序，按照团体险业绩升序排序。
//注意：不要设计键盘输入职工信息，可根据样例中提供的数据直接创建职工对象；
//
//输入格式:
//输出格式:
//各项之间用逗号“,”分隔
// 职工类实现Comparable接口
class Employee implements Comparable<Employee> {
    private int id;
    private String name;
    private String gender;
    private int groupInsurance;
    private int individualInsurance;

    // 无参构造方法
    public Employee() {
    }

    // 带参构造方法
    public Employee(int id, String name, String gender, int groupInsurance, int individualInsurance) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.groupInsurance = groupInsurance;
        this.individualInsurance = individualInsurance;
    }

    // getter和setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getGroupInsurance() {
        return groupInsurance;
    }

    public void setGroupInsurance(int groupInsurance) {
        this.groupInsurance = groupInsurance;
    }

    public int getIndividualInsurance() {
        return individualInsurance;
    }

    public void setIndividualInsurance(int individualInsurance) {
        this.individualInsurance = individualInsurance;
    }

    // 计算总业绩
    public int getTotalInsurance() {
        return groupInsurance + individualInsurance;
    }

    // 实现Comparable接口，按总业绩比较
    @Override
    public int compareTo(Employee o) {
        return Integer.compare(this.getTotalInsurance(), o.getTotalInsurance());
    }

    // 重写toString方法，方便输出
    @Override
    public String toString() {
        return id + "," + groupInsurance + "," + individualInsurance + "," + name + "," + gender;
    }
}

// 团体险业绩比较器
class GroupInsuranceComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee e1, Employee e2) {
        return Integer.compare(e1.getGroupInsurance(), e2.getGroupInsurance());
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建职工列表
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "职工1", "female", 500, 400));
        employees.add(new Employee(3, "职工3", "male", 600, 300));
        employees.add(new Employee(2, "职工2", "female", 400, 600));
        employees.add(new Employee(4, "职工4", "female", 800, 200));
        employees.add(new Employee(5, "职工5", "male", 500, 700));

        // 按总业绩升序排序
        Collections.sort(employees);
        System.out.println("编号,团险,个险,姓名,性别");
        for (Employee e : employees) {
            System.out.println(e);
        }

        // 按团体险业绩升序排序
        Collections.sort(employees, new GroupInsuranceComparator());
        System.out.println("编号,团险,个险,姓名,性别");
        for (Employee e : employees) {
            System.out.println(e);
        }
    }
}